package com.bs.service;

import com.bs.domain.AlertSub;
import com.bs.domain.AlertSubQuery;
import com.bs.domain.AlertUser;
import com.bs.domain.PartitionIp2Host;
import com.bs.repository.AlertSubRepository;
import com.bs.repository.AlertUserRepository;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by wujian
 * Date: 2017-12-08
 * Time: 15:11
 */
@Service
@Transactional
public class AlertSubService {
    @Autowired
    private AlertSubRepository alertSubRepository;

    public List<AlertSubQuery> query(AlertSub alertSub) {
        List<AlertSubQuery> alertSubQueryList = alertSubRepository.findAlertSubInfo();

        List<AlertSubQuery> conllection = alertSubQueryList.stream()
            .filter(alertSub.getUserId() != null && !alertSub.getUserId().equals("") ? x -> x.getUserId().equals(alertSub.getUserId()) : x -> true)
            .filter(alertSub.getAlertId() != null && !alertSub.getAlertId().equals("") ? x -> x.getAlertId().equals(alertSub.getAlertId()) : x -> true)
            .filter(alertSub.getModuleName() != null && !alertSub.getModuleName().equals("") ? x -> x.getModuleName().contains(alertSub.getModuleName()) : x -> true)
            .filter(alertSub.getPartitionId() != null && !alertSub.getPartitionId().equals("") ? x -> x.getPartitionId().equals(alertSub.getPartitionId()) : x -> true)
            .filter(alertSub.getMsgType() != null && !alertSub.getMsgType().equals("") ? x -> x.getMsgType().equals(alertSub.getMsgType()) : x -> true)
            .collect(Collectors.toList());
        return conllection;
    }

    public void save(AlertSub alertSub) {
        alertSubRepository.save(alertSub);
    }

    public void delete(AlertSub alertSub) {
        alertSubRepository.delete(alertSub);
    }

    /**
     * 条件查询时动态组装条件
     */
    private Specification<AlertSub> where(AlertSub alertSub) {
        return new Specification<AlertSub>() {
            @Override
            public Predicate toPredicate(Root<AlertSub> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<Predicate>();

                if (alertSub.getAlertId() != null && !alertSub.getAlertId().equals("")) {
                    predicates.add(cb.equal(root.<Long>get("alertId"), alertSub.getAlertId()));
                }
                if (alertSub.getUserId() != null && !alertSub.getUserId().equals("")) {
                    predicates.add(cb.equal(root.<String>get("userId"), alertSub.getUserId()));
                }
                if (alertSub.getPartitionId() != null && !alertSub.getPartitionId().equals("")) {
                    predicates.add(cb.equal(root.<String>get("partitionId"), alertSub.getPartitionId()));
                }
                if (alertSub.getModuleName() != null && !alertSub.getModuleName().equals("")) {
                    predicates.add(cb.like(root.<String>get("moduleName"), "%" + alertSub.getModuleName() + "%"));
                }
                if (alertSub.getMsgType() != null && !alertSub.getMsgType().equals("")) {
                    predicates.add(cb.equal(root.<Integer>get("msgType"), alertSub.getMsgType()));
                }
                return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
            }
        };
    }

}
